#pragma once
#include<iostream>
#include<cstring>
#include<string>

#define SEP " "
#define SEP_LEN strlen(SEP) //不敢使用sizeof
#define LINE_SEP "\r\n"
#define LINE_SEP_LEN strlen(LINE_SEP)

enum { OK=0, DIV_ZERO, OP_ERROR};
const std::string &enLength(const std::string &text)
{

}

const std::string &deLength(const std::string &package)
{

}

class Request
{
public:
    Request(): x(0), y(0), op(0)
    {}
    Request(int x_, int y_, char op_):x(x_), y(y_), op(op_)
    {};
    // 1. 自己写
    // 2. 用现成的

    bool serialize(std::string *out)
    {
        *out = "";
        //结构化 -> "x op y\r\n";
        std::string x_string = std::to_string(x);
        std::string y_string = std::to_string(y);
        *out = x_string;
        *out += SEP;
        *out += op;
        *out += SEP;
        *out += y_string;
        *out += LINE_SEP;
    };
    // "x op y\r\n"
    bool deserialize(const std::string &in)
    {
        // "x op y\r\n" -> 结构化
        auto left = in.find(SEP);
        auto right = in.rfind(SEP);
        if(left == std::string::npos || right == std::string::npos) return false;
        if(left == right) return false;
        if(right - (left+SEP_LEN) != 1) return false;

        std::string x_string = in.substr(0, left);
        std::string y_string = in.substr(right+SEP_LEN);
        if(x_string.empty() || y_string.empty()) return false;
        x = std::stoi(x_string);
        y = std::stoi(y_string);
        op = in[left+SEP_LEN];
    };

private:
    int x;
    int y;
    char op;
};
class Response
{
public:
    Response():exitcode(0),result(0)
    {}
    bool serialize(std::string *out)
    {};
    bool deserialize()
    {};
public:
    int exitcode; //0:计算成功，！0表示计算失败，具体是多少,定好标准
    int result; //计算结果
};
